//
//  HavingPurchasedViewController.m
//  1243
//
//  Created by 洛水寒 on 16/5/24.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "HavingPurchasedViewController.h"

#import "NetWorkTools.h"//网络请求

@interface HavingPurchasedViewController ()

@property (weak, nonatomic) IBOutlet UILabel *totalMoney;//总计金额
@property (weak, nonatomic) IBOutlet UIImageView *chooseMoney;//选择金额支付
@property (weak, nonatomic) IBOutlet UIImageView *chooseBonus;//选择积分支付


@property (weak, nonatomic) IBOutlet UILabel *couponID;//用户编号
@property (weak, nonatomic) IBOutlet UILabel *num;//出售数量
@property (weak, nonatomic) IBOutlet UILabel *price;//出售价格
@property (weak, nonatomic) IBOutlet UITextField *buyNum;//购买数量
@property (weak, nonatomic) IBOutlet UILabel *money;//金额支付
@property (weak, nonatomic) IBOutlet UILabel *bonus;//积分支付

@property (strong, nonatomic) NSString *isSelected;
@property (strong, nonatomic) NSString *Id;//交易编码

@end

@implementation HavingPurchasedViewController
{
    NSNotificationCenter *_nc;//通知中心
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"进行购买";
    
    _isSelected = @"1";
    
    //赋值
    [self setValue];
    
    //网络请求
    [self getMoney];
    
    
    _nc = [NSNotificationCenter defaultCenter];
    //注册观察者 通知的名字一致
    [_nc addObserver:self selector:@selector(saleNumDidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)saleNumDidEndEdit{
    if (![_buyNum.text isEqualToString:@""]) {
        _totalMoney.text = [NSString stringWithFormat:@"%.2f", [_buyNum.text intValue] * [_price.text floatValue]];
    }
}


- (void)setValue{
    _couponID.text = _model.UserName;
    
    _Id = [NSString stringWithFormat:@"%@", _model.Id];
    _num.text = [NSString stringWithFormat:@"%@", _model.TradeQty];
    _price.text = [NSString stringWithFormat:@"%@", _model.TradePrice];
}
- (void)getMoney{
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSString *UserName = [dict objectForKey:@"UserName"];
    
    NSDictionary *params2 = @{
                              //
                              @"Id":userNumber,
                              @"UserName":UserName
                              };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params2 AndMenthodName:@"getUserInfo" completeBlock:^(id object) {
        
        NSString *str = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:str];
        //        NSLog(@"用户信息%@",object);
        
        if (arr == nil) {
            self.money.text = @"";
            self.bonus.text = @"";
        }else if ([arr[0][@"Money"] isEqual:[NSNull null]]){
            self.money.text = @"";
        }else if ([arr[0][@"fh_Money"] isEqual:[NSNull null]]){
            self.bonus.text = @"";
        }
        else{
            self.money.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"Money"] floatValue]];
            self.bonus.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"fh_Money"] floatValue]];
        }
    }];
}

//金额支付
- (IBAction)payUseMoney:(id)sender {
    _isSelected = @"1";
    _chooseMoney.backgroundColor = [UIColor lightGrayColor];
    _chooseBonus.backgroundColor = [UIColor whiteColor];
}
//积分支付
- (IBAction)payUseBonus:(id)sender {
    _isSelected = @"2";
    _chooseBonus.backgroundColor = [UIColor lightGrayColor];
    _chooseMoney.backgroundColor = [UIColor whiteColor];
}

//确认购买
- (IBAction)confirmedBuy:(id)sender {
    //网络请求
    [self requestBuy];
}
- (void)requestBuy{
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *TradeUserId = [dict objectForKey:@"Number"];
    NSString *TradeId = _Id;
    NSString *TradeQty = _buyNum.text;
    NSString *TradePrice = _price.text;
    NSString *PayMoney = _totalMoney.text;
    NSString *PayType = _isSelected;
    
    
    
    
    NSDictionary *params = @{
                             //
                             @"TradeUserId":TradeUserId,
                             @"TradeId":TradeId,
                             @"TradeQty":TradeQty,
                             @"TradePrice":TradePrice,
                             @"PayMoney":PayMoney,
                             @"PayType":PayType
                             };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"BuyTicket" completeBlock:^(id object) {
        
        
        //提示框
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:object[@"Message"] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        NSLog(@"%@", object[@"Message"]);
        [message show];
        
    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_buyNum resignFirstResponder];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
